<?
require_once("include.php");

$me=false;
require_once("header.session.php");
require_once("header.gui.php");
require_once("header.misc.php");
require_once("header.admin.php");

global $me;


//globals
if(!$me) {
	$me=&new Person($_SESSION['id']);
}
$MenuObjects= $me->getMyMenu();


if(isset($_GET['action'])) {
	switch($_GET['action']) {
		case "changedate":
			header("Location: " . $_SERVER['PHP_SELF'] . "?id=" . $_GET['id'] . "&start=" . $_GET['syear'] . "-" . $_GET['smonth'] . "-" . $_GET['sday'] . "&end=" . $_GET['eyear'] . "-" . $_GET['emonth'] . "-" . $_GET['eday']);
			break;
	}
}
if(!isset($_GET['id'])) {
//	header("Location: attendance.php");
//	die;
	$_GET['id']=0;
}

if(!isset($_GET['start'])) {
	$_GET['start']=unixToDate(time());
}

if(!isset($_GET['end'])) {
	$_GET['end']=unixToDate(time());
}

if(unixToDate(timestampToUnix($_GET['start']))!=$_GET['start']) {
	$_GET['start']=unixToDate(time());
}

if(unixToDate(timestampToUnix($_GET['end']))!=$_GET['end']) {
	$_GET['end']=unixToDate(time());
}

function Headers() {
	
}
function Body() {
	echo "
	<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">
		<input type=\"hidden\" name=\"start\" value=\"" . $_GET['start'] . "\">
		<input type=\"hidden\" name=\"end\" value=\"" . $_GET['end'] . "\">
		<input type=\"hidden\" name=\"action\" value=\"changeperson\">
	<select name=\"id\" onchange=\"submit()\">
	<option id=\"0\"> - Select A Student - </option>\n";
		$users=getUserList("none",1);
		foreach($users as $user) {
			if(isStudent($user['people_id'])) {
				echo "<option ";
				if($_GET['id']==$user['people_id']) {
					echo "SELECTED ";
				}
				echo "value=\"" . $user['people_id'] . "\">" . stripslashes($user['people_firstname']) . " " . stripslashes($user['people_lastname']) . "</option>\n";
			}
		}
		echo "</select>
		
		<input type=\"submit\" value=\"Select Student\">
	</form>
		
	<b>Timeframe:</b> " . $_GET['start'] . " - " . $_GET['end'] . "<br>
	<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"get\">
		<input type=\"hidden\" name=\"id\" value=\"" . $_GET['id'] . "\">
		<input type=\"hidden\" name=\"action\" value=\"changedate\">
		<table><tr><td>
		START DATE
		</td><td>END DATE</td></tr>
		<tr><td>
		Month:
		<select name=\"smonth\">
		";
		for($i=1;$i<=12;$i++) {
			echo "<option value=\"" . str_pad($i, 2, "0", STR_PAD_LEFT) . "\">" . $i . "</option>\n";
		}
		echo "
		</select>
		<br>
		Day:
		<select name=\"sday\">
		";
		for($i=1;$i<=31;$i++) {
			echo "<option value=\"" . str_pad($i, 2, "0", STR_PAD_LEFT) . "\">" . $i . "</option>\n";
		}
		echo "
		</select>
		<br>
		Year:
		<select name=\"syear\">
		";
		for($i=2005;$i<date("Y");$i++) {
			echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
		}
		echo "<option SELECTED value=\"" . date("Y") . "\">" . date("Y") . "</option>\n

		</select>
		
		
		</td><td>
		Month:
		<select name=\"emonth\">
		";
		for($i=1;$i<=12;$i++) {
			echo "<option value=\"" . str_pad($i, 2, "0", STR_PAD_LEFT) . "\">" . $i . "</option>\n";
		}
		echo "
		</select>
		<br>
		Day:
		<select name=\"eday\">
		";
		for($i=1;$i<=31;$i++) {
			echo "<option value=\"" . str_pad($i, 2, "0", STR_PAD_LEFT) . "\">" . $i . "</option>\n";
		}
		echo "
		</select>
		<br>
		Year:
		<select name=\"eyear\">
		";
		for($i=2005;$i<date("Y");$i++) {
			echo "<option value=\"" . $i . "\">" . $i . "</option>\n";
		}
		echo "<option SELECTED value=\"" . date("Y") . "\">" . date("Y") . "</option>\n

		</select>
		</td></tr><tr><td colspan=\"2\">
		<input type=\"submit\" value=\"Select Timeframe\">
	</form>
	";
	if((isset($_GET['start'])) AND (isset($_GET['end'])) AND isset($_GET['id'])) {
		$tardy=array("excused"=>0,"unexcused"=>0);
		$absent=array("excused"=>0,"unexcused"=>0);
		$present=array("excused"=>0,"unexcused"=>0);
		$dismiss=array("excused"=>0,"unexcused"=>0);
		$total=0;
		for($i = 0;$i<=(abs(timestampToUnix($_GET['end'])-timestampToUnix($_GET['start']))/(24*60*60));$i++) {
			$today=explode(":",getAttendance($_GET['id'],unixToTimestamp(min(timestampToUnix($_GET['end']),timestampToUnix($_GET['start']))+(24*60*60*$i)+(12*60*60))));
			if(!noSchool(unixToTimestamp(min(timestampToUnix($_GET['end']),timestampToUnix($_GET['start']))+(24*60*60*$i)+(12*60*60)))) {
				$total++;
				${$today[0]}[$today[1]]++;
			}
		}
		echo "<br><br><u>Total Days: " . $total . "</u>";
		echo "<br>";
		echo "<b>Tardy:</b><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unexcused: " . $tardy['unexcused'] . "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Excused: " . $tardy['excused'] . "<br>";
		echo "<b>Absent:</b><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Unexcused: " . $absent['unexcused'] . "<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Excused: " . $absent['excused'] . "<br>";
		echo "<b>Present:</b> " . ($present['excused']) . "<br>";
		echo "<b>Early dismissal:</b> " . ($dismiss['excused']+$dismiss['unexcused']);
	}
	
}

include ("template.php");
